<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\BaseController;
use app\admin\model\Admin;
use app\admin\model\AuthGroup;
use app\admin\model\AuthGroupAccess;
use app\admin\model\AuthRule;
use think\facade\Db;
use think\facade\Filesystem;
use think\facade\Request;
use think\facade\View;

class Auth extends BaseController
{
    public function authDel()
    {
        $id=Request::get('id','');
        $del=AuthGroup::destroy($id);
        if ($del){
            $this->success('删除权限成功',url('Auth/auth'));
        }else{
            $this->error('网络波动删除失败');
        }
    }

    public function authEdit()
    {

        $id=Request::get('id','');
        $value=AuthGroup::getAuthGroupValue($id);
        View::assign('value',$value);
        $list=AuthRule::field(true)->select();
        View::assign('list',$list);
        if (Request::isPost()){
            $post=Request::post();
            $data=[
                'id'=>$id,
                'title'=>$post['aname'],
                'status'=>1,
                'rules'=>implode(',',$post['auths']),
            ];

            $update=AuthGroup::update($data);
            if ($update){
                $this->success('修改成功',url('Auth/auth'));
            }else{
                $this->error('网络波动,修改失败');
            }
        }
        return view();
    }
    public function auth()
    {

        $list=AuthGroup::getAuthGroupList();
        View::assign('list',$list);
        return view();
    }

    public function authAdd()
    {

        if (Request::isPost()){
            $post=Request::post();
            $data=[
                'title'=>$post['aname'],
                'status'=>1,
                'rules'=>implode(',',$post['auths']),
                'addtime'=>time()
            ];

            $add=AuthGroup::create($data);
            if ($add){
                $this->success('添加成功',url('Auth/auth'));
            }else{
                $this->error('网络波动,添加失败');
            }
        }
        $list=AuthRule::field(true)->select();
        View::assign('list',$list);
        return view();
    }

    public function user()
    {
        list($list,$page)=Admin::getUserList();
        View::assign('list',['list'=>$list,'page'=>$page]);
        return view();
    }

    public function userAdd()
    {
        $authInfo=AuthGroup::select();
        View::assign('authInfo',$authInfo);

        //查询需要修改的数据 赋值到HTML
        if (Request::isPost()){
            //接参
            $post = Request::post();
            $data=[];

            $data['name']=$post['name'];
            $image=Auth::upload('logo','admin_img','md5');
            if (!empty($image)){
                $data['headimg']=$image;
            }


                $data['pwd']=md5('123456');
            $Add=Admin::AddUser($data);
            $uid=$Add->id;
            if (!empty($post['author'])){
                $data2=[];
                $data2['uid']=$uid;
                $data2['group_id']=$post['author'];
                $authSet=AuthGroupAccess::setAuth($data2);
            }


            if ($Add){
                $this->success('添加成功',url('Auth/user'));
            }else{
                $this->error('添加失败');
            }

        }
        return view();
    }

    public function userDel()
    {
        $id=Request::get('id','');
        $del=Admin::destroy($id);
        if ($del){
            $this->success('删除成功',url('Auth/user'));
        }else{
            $this->error('网络波动,删除失败');
        }
    }
    public function userEdit()
    {

        //查询需要修改的数据 赋值到HTML
        if (Request::isPost()){
            //接参
            $post = Request::post();
            $data=[];
            $data['id']=$post['id'];

            $image=Auth::upload('logo','admin_img','md5');
            if (!empty($image)){
                $data['headimg']=$image;
            }
            if (!empty($post['reset'])){
                $data['pwd']=md5($post['reset']);
            }
            if (!empty($post['author'])){
                $data2=[];
                $data2['uid']=$post['id'];
                $data2['group_id']=$post['author'];
                $authSet=AuthGroupAccess::setAuth($data2);
            }
            $update=Admin::updateUser($data);

            if ($update){
                $this->success('修改成功',url('Auth/user'));
            }else{
                $this->error('修改失败');
            }
        }else{
            //查询更新的数据
            //接ID
            $id = Request::get('id');
            View::assign('id',$id);
                $AuthValue=AuthGroupAccess::getAuthValue($id);
            View::assign('AuthValue',$AuthValue);
            $userInfo = \app\admin\model\Admin::find($id)->toArray();
            $authInfo=AuthGroup::select();
            View::assign('authInfo',$authInfo);
            View::assign('userInfo',$userInfo);
            return view();
        }
    }
}